package com.yequan.leetcode.array.rotate_189;

import com.yequan.leetcode.array.ArrayUtil;

/**
 * //给定一个数组，将数组中的元素向右移动 k 个位置，其中 k 是非负数。
 * //
 * // 示例 1:
 * //
 * // 输入: [1,2,3,4,5,6,7] 和 k = 3
 * //输出: [5,6,7,1,2,3,4]
 * //解释:
 * //向右旋转 1 步: [7,1,2,3,4,5,6]
 * //向右旋转 2 步: [6,7,1,2,3,4,5]
 * //向右旋转 3 步: [5,6,7,1,2,3,4]
 * //
 * //
 * // 示例 2:
 * //
 * // 输入: [-1,-100,3,99] 和 k = 2
 * //输出: [3,99,-1,-100]
 * //解释:
 * //向右旋转 1 步: [99,-1,-100,3]
 * //向右旋转 2 步: [3,99,-1,-100]
 * //
 * // 说明:
 * //
 * //
 * // 尽可能想出更多的解决方案，至少有三种不同的方法可以解决这个问题。
 * // 要求使用空间复杂度为 O(1) 的 原地 算法。
 *
 * @author : Administrator
 * @date : 2020/3/12
 */
public class RecodeRotate {

    public static void main(String[] args) {
        int[] nums1 = {1, 2, 3, 4, 5, 6, 7};
        int[] nums2 = {-1, -100, 3, 99};
        RecodeRotate recodeRotate = new RecodeRotate();
        //直接法测试
//        SimpleRotate simpleRotate = recodeRotate.new SimpleRotate();
//        simpleRotate.rotate1(nums1, 10);
//        ArrayUtil.printArray(nums1);

        //临时数组法
//        DoubleArrayRotate doubleArrayRotate = recodeRotate.new DoubleArrayRotate();
//        doubleArrayRotate.rotate2(nums2, 2);
//        ArrayUtil.printArray(nums2);

        //测试环状替换法
        CyclicReplaceRotate cyclicReplaceRotate = recodeRotate.new CyclicReplaceRotate();
        cyclicReplaceRotate.rotate4(nums2, 3);
        ArrayUtil.printArray(nums2);

    }

    /**
     * 直接法:
     * <p>
     * 移动k次
     * 1.每次先保存 nums[nums.length-1]元素
     * 2.每次移动 0 --- nums.length-2的元素到 1 --- nums.length-1
     * 3.nums[0]=temp
     * <p>
     * 复杂度
     * 时间复杂度: O(k*n)
     * 空间复杂度: O(1)
     */
    public class SimpleRotate {

        public void rotate1(int[] nums, int k) {
            if (null == nums || nums.length == 0 || k < 0) {
                return;
            }
            for (int i = 0; i < (k % nums.length); i++) {
                int temp = nums[nums.length - 1];
                for (int j = nums.length - 1; j > 0; j--) {
                    nums[j] = nums[j - 1];
                }
                nums[0] = temp;
            }
        }

        public void rotate2(int[] nums, int k) {
            if (null == nums || nums.length == 0 || k < 0) {
                System.err.println("参数非法");
                return;
            }
            for (int i = 0; i < (k % nums.length); i++) {
                int temp = nums[nums.length - 1];
                for (int j = nums.length - 1; j > 0; j--) {
                    nums[j] = nums[j - 1];
                }
                nums[0] = temp;
            }
        }

        public void rotate3(int[] nums, int k) {
            if (null == nums || nums.length == 0 || k < 0) {
                return;
            }
            for (int i = 0; i < (k % nums.length); i++) {
                int lastTailNum = nums[nums.length - 1];
                for (int j = nums.length - 1; j > 0; j--) {
                    nums[j] = nums[j - 1];
                }
                nums[0] = lastTailNum;
            }
        }

        public void rotate4(int[] nums, int k) {
            if (null == nums || nums.length == 0 || k < 0) {
                return;
            }
            for (int i = 0; i < (k % nums.length); i++) {
                int lastTailNum = nums[nums.length - 1];
                for (int j = nums.length - 1; j > 0; j--) {
                    nums[j] = nums[j - 1];
                }
                nums[0] = lastTailNum;
            }
        }
    }

    /**
     * 临时数组法:
     * <p>
     * 基础知识:
     * 数组的位置下标表示: i = (i % nums.length)
     * 向后旋转k后位置下标变化: ((i+k)%length) = i
     * <p>
     * 算法思想:
     * 1.根据 向后旋转k后位置下标变化: index = ((i+k)%length) 公式,将对应位置的数据插入新的数组中
     * 2.将新数组数据插入原数组中
     * <p>
     * 复杂度
     * 时间复杂度: O(n)
     * 空间复杂度: O(n)
     */
    public class DoubleArrayRotate {

        public void rotate1(int[] nums, int k) {
            if (null == nums || nums.length == 0 || k < 0) {
                return;
            }
            int[] temp = new int[nums.length];
            for (int i = 0; i < temp.length; i++) {
                temp[(i + k) % nums.length] = nums[i];
            }
            for (int i = 0; i < nums.length; i++) {
                nums[i] = temp[i];
            }
        }

        public void rotate2(int[] nums, int k) {
            if (null == nums || nums.length == 0 || k < 0) {
                return;
            }
            int[] temp = new int[nums.length];
            for (int i = 0; i < temp.length; i++) {
                temp[(i + k) % nums.length] = nums[i];
            }

            for (int i = 0; i < nums.length; i++) {
                nums[i] = temp[i];
            }
        }

        public void rotate3(int[] nums, int k) {
            if (null == nums || nums.length == 0 || k < 0) {
                return;
            }
            int[] temp = new int[nums.length];
            for (int i = 0; i < temp.length; i++) {
                //整体享有移动k位
                temp[(i + k) % nums.length] = nums[i];
            }
            for (int i = 0; i < nums.length; i++) {
                nums[i] = temp[i];
            }
        }

        public void rotate4(int[] nums, int k) {
            if (null == nums || nums.length == 0 || k < 0) {
                return;
            }
            int[] temp = new int[nums.length];
            for (int i = 0; i < temp.length; i++) {
                temp[(i + k) % nums.length] = nums[i];
            }
            for (int i = 0; i < nums.length; i++) {
                nums[i] = temp[i];
            }
        }

    }

    /**
     * 环状替换法
     * 算法核心: 数组中元素向右移动k次后的位置是 (i+k)%nums.length
     * 数组中每一个元素都需要替换一次
     * <p>
     * 复杂度
     * 时间复杂度: O(n)
     * 空间复杂度: O(1)
     */
    public class CyclicReplaceRotate {

        public void rotate1(int[] nums, int k) {
            if (null == nums || nums.length == 0 || k < 0) {
                return;
            }
            k = k % nums.length;
            /**
             * 记录已替换的数的个数
             */
            int count = 0;
            for (int startIndex = 0; count < nums.length; startIndex++) {
                //从start位置开始替换
                int currentIndex = startIndex;
                int preNum = nums[startIndex];
                do {
                    int nextIndex = (currentIndex + k) % nums.length;
                    int nextNum = nums[nextIndex];
                    nums[nextIndex] = preNum;
                    preNum = nextNum;
                    currentIndex = nextIndex;
                    count++;
                } while (startIndex != currentIndex);//结束条件: 当第一个元素nums[start]要第二次替换时结束
            }
        }

        public void rotate2(int[] nums, int k) {
            if (null == nums || nums.length == 0 || k < 0) {
                return;
            }

            k = k % nums.length;
            int count = 0;
            for (int startIndex = 0; count < nums.length; startIndex++) {
                int currentIndex = startIndex;
                int currentNum = nums[currentIndex];
                do {
                    int nextIndex = (currentIndex + k) % nums.length;
                    int nextNum = nums[nextIndex];
                    nums[nextIndex] = currentNum;
                    currentIndex = nextIndex;
                    currentNum = nextNum;
                    //替换完成一个数进行+1
                    count++;
                } while (startIndex != currentIndex);
            }

        }

        public void rotate3(int[] nums, int k) {
            if (null == nums || nums.length == 0 || k < 0) {
                return;
            }
            k = k % nums.length;
            int count = 0;
            for (int startIndex = 0; count < nums.length; startIndex++) {
                int currentIndex = startIndex;
                int currentNum = nums[currentIndex];
                do {
                    int nextIndex = (currentIndex + k) % nums.length;
                    int nextNum = nums[nextIndex];
                    nums[nextIndex] = currentNum;
                    currentIndex = nextIndex;
                    currentNum = nextNum;
                    count++;
                } while (startIndex != currentIndex);
            }
        }

        public void rotate4(int[] nums, int k) {
            if (null == nums || nums.length == 0 || k < 0) {
                return;
            }
            k = k % nums.length;
            int count = 0;
            for (int startIndex = 0; count < nums.length; startIndex++) {
                int currentIndex = startIndex;
                int currentNum = nums[currentIndex];
                do {
                    int nextIndex = (currentIndex + k) % nums.length;
                    int nextNum = nums[nextIndex];
                    nums[nextIndex] = currentNum;
                    currentIndex = nextIndex;
                    currentNum = nextNum;
                    count++;
                } while (startIndex != currentIndex);
            }
        }

    }

}
